package com.aptemo.webetek.server.data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.annotations.Element;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.aptemo.webetek.client.enums.EnumTeamStatus;
import com.aptemo.webetek.shared.TeamBean;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.gwt.user.client.rpc.IsSerializable;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class UserGroup implements IsSerializable
{

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key					key;

	private String				sid;

	@Persistent
	@Element(dependent = "true")
	private List<BetLabel>		labels					= new ArrayList<BetLabel>();

	@Persistent
	@Element(dependent = "true")
	private List<Bet>			bets					= new ArrayList<Bet>();

	@Persistent
	@Element(dependent = "true")
	private List<WebetekUser>	players					= new ArrayList<WebetekUser>();

	@Persistent
	private Key					ownerKey;

	@Persistent
	private String				ownerEmail;

	@Persistent
	private String				name;

	@Persistent
	private String				description;

	@Persistent
	private Boolean				isPublic				= false;

	@Persistent
	private Date				created;

	@Persistent
	private Double				minimumStake;

	@Persistent
	private Double				maximumStake;

	@Persistent
	private String				teamRules;

	@Persistent
	private Boolean				placeBetNotifications	= false;

	@Persistent
	private EnumTeamStatus		status					= EnumTeamStatus.ACTIVE;

	private Boolean				allowDuplicateBets		= false;

	public UserGroup(String name)
	{
		setName(name);
		created = new Date();
	}

	public String getSid()
	{
		if (sid == null)
		{
			this.sid = KeyFactory.keyToString(key);
		}
		return sid;
	}

	public Key getKey()
	{
		return key;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public Key getOwnerKey()
	{
		return ownerKey;
	}

	public void setOwnerKey(Key owner, String email)
	{
		this.ownerKey = owner;
		this.ownerEmail = email;
	}

	public void setPublic(boolean isPublic)
	{
		this.isPublic = isPublic;
	}

	public boolean isPublic()
	{
		return isPublic;
	}

	public void setDescription(String description)
	{
		this.description = description;
	}

	public String getDescription()
	{
		return description;
	}

	// public void setPlayers(Set<WebetekUser> players)
	// {
	// this.players = players;
	// }

	public List<WebetekUser> getPlayers()
	{
		return players;
	}

	public boolean addPlayer(WebetekUser user)
	{
		if (!players.contains(user))
		{
			this.players.add(user);
			return true;
		}
		return false;
		// if (user.getRole() == EnumRole.EDITOR)
		// {
		// this.ownerKey = user.getKey();
		// }
	}

	public void removePlayer(WebetekUser player)
	{
		players.remove(player);
	}

	// private WebetekUser getOwnerFromPlayerList()
	// {
	// // get owner...
	// for (WebetekUser player : players)
	// {
	// if (player.getRole() == EnumRole.EDITOR)
	// {
	// return player;
	// }
	// }
	// return null;
	// }

	public void addEvent(Bet agame)
	{
		bets.add(agame);
	}

	public void removeEvent(Bet game)
	{
		bets.remove(game);
	}

	public List<Bet> getEvents()
	{
		return bets;
	}

	public List<BetLabel> getLabels()
	{
		return labels;
	}

	public void setLabels(List<BetLabel> labels)
	{
		this.labels = labels;
	}

	public boolean addLabel(BetLabel alabel)
	{
		// check if label exists...
		// for (BetLabel label : labels)
		// {
		// if (label.getName().equals(alabel))
		// {
		// return false;
		// }
		// }
		labels.add(alabel);
		return true;
	}

	public void removeLabel(BetLabel label)
	{
		labels.remove(label);
	}

	public Date getCreated()
	{
		return created;
	}

	public BetLabel findLabel(String labelTitle)
	{
		for (BetLabel label : labels)
		{
			if (labelTitle.equals(label.getName()))
			{
				return label;
			}
		}
		return null;
	}

	public TeamBean toBean()
	{
		TeamBean bean = new TeamBean(name);
		bean.setKey(getSid());
		bean.setPublic(isPublic);
		bean.setDescription(description);
		bean.setTeamRules(teamRules);
		bean.setMinStake(minimumStake);
		bean.setMaxStake(maximumStake);
		bean.setPlaceBetNotifications(getPlaceBetNotifications());
		bean.setAllowDuplicateBets(isAllowDuplicateBets());
		bean.setOwnerEmail(ownerEmail);
		bean.setStatus(getStatus());

		// if (ownerKey != null)
		// {
		// String ok = KeyFactory.keyToString(ownerKey);
		// bean.setOwnerId(ok);
		// }
		// get owner email...
		// WebetekUser owner = getOwner();
		// if (owner != null)
		// {
		// // bean.setOwnerId(owner.getEmail());
		// bean.setOwner(owner.toBean());
		// }

		// List<UserBean> tmp = new ArrayList<UserBean>();
		// for (WebetekUser player : players)
		// {
		// tmp.add(player.toBean());
		// }
		// bean.setPlayers(tmp);

		// List<BetBean> tbets = new ArrayList<BetBean>();
		// for (Bet bet : bets)
		// {
		// tbets.add(bet.toBean());
		// }
		// bean.setBets(tbets);

		return bean;
	}

	public double getMinimumStake()
	{
		if (minimumStake == null)
		{
			return 0.0;
		}
		else
		{

			return minimumStake;
		}
	}

	public double getMaximumStake()
	{
		if (maximumStake == null)
		{
			return Double.POSITIVE_INFINITY;
		}
		else
		{

			return maximumStake;
		}
	}

	public void setMinimumStake(Double minimumStake)
	{
		this.minimumStake = minimumStake;
	}

	public String getTeamRules()
	{
		return teamRules;
	}

	public void setTeamRules(String teamRules)
	{
		this.teamRules = teamRules;
	}

	public void setMaximumStake(double maxStake)
	{
		this.maximumStake = maxStake;
	}

	public Boolean isAllowDuplicateBets()
	{
		if (allowDuplicateBets == null)
		{
			return false;
		}
		return allowDuplicateBets;
	}

	public void setAllowDuplicateBets(Boolean allowDuplicateBets)
	{
		this.allowDuplicateBets = allowDuplicateBets;
	}

	public Boolean getPlaceBetNotifications()
	{
		if (placeBetNotifications == null)
		{
			return false;
		}
		return placeBetNotifications;
	}

	public void setPlaceBetNotifications(Boolean placeBetNotifications)
	{
		this.placeBetNotifications = placeBetNotifications;
	}

	public String getOwnerEmail()
	{
		return ownerEmail;
	}

	public EnumTeamStatus getStatus()
	{
		if (status == null)
		{
			return EnumTeamStatus.ACTIVE;
		}
		return status;
	}

	public void setStatus(EnumTeamStatus status)
	{
		this.status = status;
	}

	// public String getOwnerEmail()
	// {
	// // remove later...
	// if (ownerEmail == null)
	// {
	// WebetekUser owner = getOwnerFromPlayerList();
	// if (owner != null)
	// {
	// ownerEmail = owner.getEmail();
	// }
	// }
	// return ownerEmail;
	// }
	//
	// public void setOwnerEmail(String ownerEmail)
	// {
	// this.ownerEmail = ownerEmail;
	// }

}
